unit uConfig;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Buttons, ComCtrls, uConst, CheckLst;

type
  TfConfig = class(TForm)
    RzPanel1: TPanel;
    RzBitBtn1: TBitBtn;
    RzBitBtn2: TBitBtn;
    RzListBox1: TPanel;
    btnFangxiang: TSpeedButton;
    btnDict: TSpeedButton;
    btnText: TSpeedButton;
    btnWeb: TSpeedButton;
    btnExam: TSpeedButton;
    RzPanel2: TPanel;
    PageCtrl: TPageControl;
    TabSheet1: TTabSheet;
    Bevel3: TBevel;
    RzLabel6: TLabel;
    RzLabel1: TLabel;
    RzLabel2: TLabel;
    cboxlangSrc: TComboBox;
    cboxlangTar: TComboBox;
    TabSheet2: TTabSheet;
    Bevel2: TBevel;
    RzLabel9: TLabel;
    clstDict: TCheckListBox;
    TabSheet3: TTabSheet;
    Bevel4: TBevel;
    RzLabel3: TLabel;
    clstText: TCheckListBox;
    TabSheet5: TTabSheet;
    Bevel5: TBevel;
    RzLabel4: TLabel;
    clstExam: TCheckListBox;
    TabSheet4: TTabSheet;
    Bevel1: TBevel;
    RzLabel5: TLabel;
    RzLabel8: TLabel;
    cboxEngine: TComboBox;
    SpeedButton1: TSpeedButton;
    TabSheet6: TTabSheet;
    cQuci: TCheckBox;
    cHuaci: TCheckBox;
    procedure btnFangxiangClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cboxlangSrcChange(Sender: TObject);
    procedure cboxlangTarChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

procedure SysConfig(ATag: Integer);

implementation

{$R *.dfm}

var
  LastButton: TSpeedButton=nil;

procedure SysConfig(ATag: Integer);
begin
  with TfConfig.Create(nil)do
  try
    case ATag of
      0: LastButton := btnFangxiang;
      1: LastButton := btnDict;
      2: LastButton := btnText;
      3: LastButton := btnExam;
      4: LastButton := btnWeb;
    end;

    LastButton.Font.Style := [ fsBold ];
    PageCtrl.ActivePageIndex := LastButton.Tag;
    ShowModal;
  finally
    Free;
  end;
end;

procedure TfConfig.btnFangxiangClick(Sender: TObject);
begin
  if LastButton<>nil then
    LastButton.Font.Style := [ ];
  LastButton := TSpeedButton(Sender);
  LastButton.Font.Style := [ fsBold ];
  PageCtrl.ActivePageIndex := LastButton.Tag;
end;

procedure TfConfig.cboxlangSrcChange(Sender: TObject);
begin
  if cboxlangTar.ItemIndex=cboxlangSrc.ItemIndex then
  begin
    if cboxlangSrc.ItemIndex=0 then
      cboxlangTar.ItemIndex := 1
    else
      cboxlangTar.ItemIndex := 0;
  end;
end;

procedure TfConfig.cboxlangTarChange(Sender: TObject);
begin
  if cboxlangTar.ItemIndex=cboxlangSrc.ItemIndex then
  begin
    if cboxlangTar.ItemIndex=1 then
      cboxlangSrc.ItemIndex := 0
    else
      cboxlangSrc.ItemIndex := 1;
  end;
end;

procedure TfConfig.FormClose(Sender: TObject; var Action: TCloseAction);
var
  idx: Integer;
begin
  if ModalResult=mrOK then
  begin
    SysCfg.LangSrc := cLangCodes[cboxlangSrc.ItemIndex];
    SysCfg.LangTar := cLangCodes[cboxlangTar.ItemIndex];
    SysCfg.SearchEngine := cboxEngine.Text;
    SysCfg.UpdateEngine;

    for idx := 0 to clstDict.Count - 1 do
    begin
      if clstDict.Checked[idx] then
        SysCfg.DictTSlst.ValueFromIndex[idx] := '1'
      else
        SysCfg.DictTSlst.ValueFromIndex[idx] := '0';
    end;
    for idx := 0 to clstText.Count - 1 do
    begin
      if clstText.Checked[idx] then
        SysCfg.TextTSlst.ValueFromIndex[idx] := '1'
      else
        SysCfg.TextTSlst.ValueFromIndex[idx] := '0';
    end;
    for idx := 0 to clstExam.Count - 1 do
    begin
      if clstExam.Checked[idx] then
        SysCfg.ExamTSlst.ValueFromIndex[idx] := '1'
      else
        SysCfg.ExamTSlst.ValueFromIndex[idx] := '0';
    end;

    SysCfg.Quci := cQuci.Checked;
    SysCfg.Huaci := cHuaci.Checked;

    SysCfg.SaveCfg;
  end;
end;

procedure TfConfig.FormCreate(Sender: TObject);
var
  idx: Integer;
begin
  for idx := 0 to PageCtrl.PageCount-1 do
    PageCtrl.Pages[idx].TabVisible := False;
  for idx := 0 to SysCfg.DictTSlst.Count - 1 do
  begin
    clstDict.Items.Add(SysCfg.DictTSlst.Names[idx]);
    clstDict.Checked[idx] := SysCfg.DictTSlst.ValueFromIndex[idx]='1';
  end;
//    clstDict.AddEx(SysCfg.DictTSlst.Names[idx], SysCfg.DictTSlst.ValueFromIndex[idx]='1', True);
  for idx := 0 to SysCfg.TextTSlst.Count - 1 do
  begin
    clstText.Items.Add(SysCfg.TextTSlst.Names[idx]);
    clstText.Checked[idx] := SysCfg.TextTSlst.ValueFromIndex[idx]='1';
  end;
//    clstText.AddEx(SysCfg.TextTSlst.Names[idx], SysCfg.TextTSlst.ValueFromIndex[idx]='1', True);
  for idx := 0 to SysCfg.ExamTSlst.Count - 1 do
  begin
    clstExam.Items.Add(SysCfg.ExamTSlst.Names[idx]);
    clstExam.Checked[idx] := SysCfg.ExamTSlst.ValueFromIndex[idx]='1';
  end;
//    clstExam.AddEx(SysCfg.ExamTSlst.Names[idx], SysCfg.ExamTSlst.ValueFromIndex[idx]='1', True);
  cboxlangSrc.ItemIndex := GetLangCodeIndex(SysCfg.LangSrc);
  cboxlangTar.ItemIndex := GetLangCodeIndex(SysCfg.LangTar);
  cboxEngine.ItemIndex := cboxEngine.Items.IndexOf(SysCfg.SearchEngine);

  cQuci.Checked := SysCfg.Quci;
  cHuaci.Checked := SysCfg.Huaci;
end;

end.
